<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable" style="padding: 0">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" :disabled="isCreate"> 新增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <FieldManageDrawer @register="registerDrawer" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { FieldManage } from '@/api/resource/resourceAllApi';
  import { useDrawer } from '@/components/Drawer';
  import FieldManageDrawer from './FieldManageDrawer.vue';
  import { columns, searchFormSchema } from './util/field.manage.data';
  import { ref } from 'vue';
  import { PageWrapper } from '@/components/Page';

  defineOptions({ name: 'FieldManage' });
  const props = defineProps({
    beforeFetch: Function,
  });
  const { pageList, del } = FieldManage();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const isCreate = ref(false);
  const [registerTable, { reload }] = useTable({
    title: '字段列表',
    api: pageList,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    actionColumn: {
      width: 80,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
      fixed: undefined,
    },
    beforeFetch: (values) => {
      props.beforeFetch(values);
      return values;
    },
  });

  function handleCreate() {
    const record = {};
    props.beforeFetch(record);
    openDrawer(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete(record: Recordable) {
    await del(record.fieldId);
    await reload();
  }

  function handleSuccess() {
    reload();
  }
</script>
